package com.shijiu.controller;

import com.shijiu.annotation.RepeatSubmit;
import com.shijiu.pojo.Food;
import com.shijiu.service.FoodService;
import com.shijiu.utils.UserThreadLocal;
import com.shijiu.vo.PageParams;
import com.shijiu.common.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
/**
 * @Author xlj
 * @Date Created in  2023/3/18 14:30
 * @DESCRIPTION:
 * @Version V1.0
 */
@RestController
@RequestMapping("food")
@Transactional(rollbackFor = Exception.class)
public class FoodController {

    @Autowired
    private FoodService foodService;

    @ApiOperation("食物列表")
    @PostMapping("foodlist")
    public Result getFoodList(@RequestBody PageParams pageParams){
        return foodService.queryFoodList(pageParams);
    }

    @ApiOperation("更新食物")
    @RepeatSubmit(interval = 2000, message = "接口请求过于频繁")
    @PostMapping("update")
    public Result updateFood(@RequestBody Food food){
        System.out.println(food);
        return Result.success(foodService.updateById(food));
    }

    @ApiOperation("保存蛋糕")
    @RepeatSubmit(interval = 2000, message = "接口请求过于频繁")
    @PostMapping("save")
    public Result saveFood(@RequestBody Food food){
        food.setShopId((Integer) UserThreadLocal.get().get("shopId"));
        return Result.success(foodService.saveOrUpdate(food));
    }
    @ApiOperation("根据id 删除蛋糕")
    @RepeatSubmit(interval = 2000, message = "接口请求过于频繁")
    @GetMapping("remove/{id}")
    public Result removeFood(@PathVariable("id") Integer id){
        return Result.success(foodService.removeById(id));
    }
}
